******************************************************************************************
* Jonathan Slapin 1/30/2006  
* Jensen Koenig Slapin paper 
* incorporate pids from Chapel hill data
* runs ordered logit models on choices about institutional rules as a function of party prefs and institutions
* revised JBS 02/16/2006
* revised JBS 02/06/2007
*******************************************************************************************

clear
version 8.2

*change working directory
capture cd "C:\Documents and Settings\Jonathan Slapin\My Documents\papers and diss\Laeken\JKS paper"

******************************************************************************
* Generate government position on foreign policy variable
******************************************************************************

use "C:\Documents and Settings\Jonathan Slapin\My Documents\Data\Chapel hill\chapelhill_2002_revised.dta" 
keep part_id foreign 
rename foreign forgovpos

sort part_id

save temp1, replace

use govparties, clear  
* This dataset contain only the parties in government and their party ids

rename partyid part_id
sort part_id

merge part_id using temp1

keep if _merge==3

collapse country forgovpos, by(countryname)  
* This gives me the average gov position for each country

save temp2, replace


* Load party id data from Chris
use "C:\Documents and Settings\Jonathan Slapin\My Documents\Data\eu_del_pids_clean"

* gen variable "pid" which takes on the Chapel Hill values
rename party_identification partyid
gen pid=.

* Austria
replace pid=1301 if country==1& partyid=="SPO (Social Democrat)"
replace pid=1302 if country==1& partyid=="�VP (Christian Democrat)"

* Belgium 
replace pid=103 if country==3& partyid=="SPA (Socialist)"
replace pid=107 if country==3& partyid=="VLD (Liberal)"
replace pid=104 if country==3& partyid=="Ecolo (Green)"

* Bulgaria
replace pid=2001 if country==2& partyid=="Mouvement national Sim�on II (Liberal Democrat)"
replace pid=2002 if country==2& partyid=="UDF (Christian Democrat)"

* Cyprus not in Chapel Hill data
* Czech Republic (parties changed- not bothering with this now)

* Denmark
replace pid=215 if country==6& partyid=="Dansk Folkeparti (Radical R)"
replace pid=202 if country==6& partyid=="Radikale Venstre (Liberal)"
replace pid=211 if country==6& partyid=="Venstre (Agrarian)"

* Estonia not in CH data

*Finland
replace pid=1401 if country==8& partyid=="SDP (Social Democrat)"

* France- Here I assume that the position of the UMP is the same as the former RPR
* perhaps there is a better assumption we could make
replace pid=602 if country==9& partyid=="PS (Socialist)"
replace pid=609 if country==9& partyid=="UMP (Christian Democrat)"

* Germany
replace pid=306 if country==10& partyid=="PDS (Socialist)"
replace pid=301 if country==10& partyid=="CDU (Christian Democrat)"
replace pid=308 if country==10& partyid=="CSU (Christian Democrat)"

* Greece
replace pid=402 if country==11& partyid=="Nea Dimokratia (Conservative)"
replace pid=403 if country==11& partyid=="Sinaspismos (Radical Left)"
replace pid=401 if country==11& partyid=="PASOK (Socialist)"

* Hungary
replace pid=2302 if country==12& partyid=="Fidesz (Christian Democrat)"
replace pid=2304 if country==12& partyid=="SDS (Liberal)"
replace pid=2301 if country==12& partyid=="Hungarian Socialist (Socialist)"

* Ireland 
replace pid=701 if country==13& partyid=="FF (Conservative)"
replace pid=703 if country==13& partyid=="Labour (Socialist)"

*Italy 
* UDC is a merger of the CCD and CDU parties. Here I assume their 
* position is the same as the CDU
replace pid=825 if country==14& partyid=="Rinnovamento Italiano (None)"
replace pid=805 if country==14& partyid=="AN (Radical Right)"
replace pid=811 if country==14& partyid=="Lega Nord (Regional)"
replace pid=818 if country==14& partyid=="UDC (Christian Democrat)"

* Luxembourg not in chapel hill data

*Portugal
replace pid=1205 if country==20& partyid=="PS (Socialist)"
replace pid=1206 if country==20& partyid=="PPD/PSD (Christian Democratic)"

* Spain
replace pid=501 if country==24& partyid=="PSOE (Socialist)"

* Sweden
replace pid=1602 if country==25& partyid=="SAP (Social Democrat)"

* Netherlands
replace pid=1004 if country==18& partyid=="D66 (Liberal)"
replace pid=1002 if country==18& partyid=="PvdA (Socialist)"
replace pid=1003 if country==18& partyid=="VVD (Liberal)"
replace pid=1001 if country==18& partyid=="CDA (Christian Democrat)"

*UK
replace pid=1102 if country==27& partyid=="Labour (Socialist)"
replace pid=1105 if country==27& partyid=="SNP (Regional)"
replace pid=1101 if country==27& partyid=="Conservative"

* Merge in Chapel Hill data and Koenig Laeken data

rename pid part_id
sort country part_id
save temp3, replace

joinby part_id using "C:\Documents and Settings\Jonathan Slapin\My Documents\Data\Chapel hill\chapelhill_2002_revised.dta" 
keep name country country_name eumember part_id party salience foreign position

save temp4, replace
joinby name country using "C:\Documents and Settings\Jonathan Slapin\My Documents\Data\tkoenig\UppsalaJBS.dta"

keep name country country_name part_id eumember party salience foreign position institut q3a_1 q3a_2 q3a_3 q3a_7 q4a q6a


* drop accession country delegates
drop if eumember~=1

* fix strange rounding for q6a
gen q6a1=".3" if q6a>.25 & q6a<.5
replace q6a1=".6" if q6a>.5 & q6a<.75
replace q6a1="0" if q6a==0
drop q6a

destring q6a1, replace
rename q6a1 q6a

* Create index of support for foreign policy based on delegates survey responses

recode q3a_1 q3a_2 q3a_3 q3a_7 (0=-1) (.5=0)

* Create index of foreign policy support
gen cfspindex=q3a_1+q3a_2+q3a_3+q3a_7

corr foreign q6a cfspindex

joinby country using temp2

* include member state population

gen pop=10.3 if country_name=="Belgium"
replace pop=5.4 if country_name=="Denmark"
replace pop=82.4 if country_name=="Germany"
replace pop=11 if country_name=="Greece"
replace pop=41 if country_name=="Spain"
replace pop=61.2 if country_name=="France"
replace pop=3.9 if country_name=="Ireland"
replace pop=57 if country_name=="Italy"
replace pop=0.4 if country_name=="Luxembourg"
replace pop=16.1 if country_name=="The Netherlands"
replace pop=8.1 if country_name=="Austria"
replace pop=10.3 if country_name=="Portugal"
replace pop=5.2 if country_name=="Finland"
replace pop=8.9 if country_name=="Sweden"
replace pop=59.2 if country_name=="United Kingdom"

* twoway (scatter q6a pop, mlabel(party) leg(off) xtitle("Population in Millions") ytitle("Qualified Majority Voting")) (lfit q6a pop) 
* graph save graph1.gph, replace

* twoway (scatter q4a pop, mlabel(party) leg(off) xtitle("Population in Millions") ytitle("Foreign Minister")) (lfit q4a pop)
* graph save graph2.gph, replace

* graph combine graph1.gph graph2.gph, ycommon

* create dummy vars for countries and institutional positions
xi i.country i.position i.institut

* Run several potential models
ologit q6a foreign cfspindex  _Iinstitut_2 _Iinstitut_3 
ologit q6a foreign cfspindex  _Iposition_3 _Iposition_4 _Iposition_5  

ologit q4a foreign cfspindex  _Iinstitut_2 _Iinstitut_3 
ologit q4a foreign cfspindex  _Iposition_3 _Iposition_4 _Iposition_5  

* generate dummy variables for and against Iraq war
gen supportus=0
replace supportus=1 if country==6|country==14|country==24|country==2|country==18|country==20

save temp5, replace

* create some scatter plots
twoway (scatter q6a cfspindex, mlabel(party)) (lfit q6a cfspindex)
twoway (scatter q6a foreign, mlabel(name)) (lfit q6a foreign)


* use Clarify to get predicted values for quantities of interest

*************Models with q4 as dependent variable************************
******************************************************************************

estsimp ologit q4a foreign q3a_1 q3a_2 q3a_7 _Iinstitut_2 _Iinstitut_3 supportus forgovpos 

**change foreign, supportus==1 **

setx foreign min forgovpos median q3a_1 median q3a_2 median q3a_7 median  _Iinstitut_2 0  _Iinstitut_3 0 supportus 1
simqi, pr

setx foreign mean forgovpos median q3a_1 median q3a_2 median q3a_7 median  _Iinstitut_2 0  _Iinstitut_3 0 supportus 1
simqi, pr

setx foreign max forgovpos median q3a_1 median q3a_2 median q3a_7 median  _Iinstitut_2 0  _Iinstitut_3 0 supportus 1
simqi, pr

**change foreign, supportus==0**

setx foreign min forgovpos median q3a_1 median q3a_2 median q3a_7 median  _Iinstitut_2 0  _Iinstitut_3 0 supportus 0
simqi, pr

setx foreign mean forgovpos median q3a_1 median q3a_2 median q3a_7 median  _Iinstitut_2 0  _Iinstitut_3 0 supportus 0
simqi, pr

setx foreign max forgovpos median q3a_1 median q3a_2 median q3a_7 median  _Iinstitut_2 0  _Iinstitut_3 0 supportus 0
simqi, pr

**change q3a_7, supportus==0, foreign==mean**

setx foreign mean forgovpos median q3a_1 median q3a_2 median q3a_7 -1  _Iinstitut_2 0  _Iinstitut_3 0 supportus 0
simqi, pr

setx foreign mean forgovpos median q3a_1 median q3a_2 median q3a_7 1  _Iinstitut_2 0  _Iinstitut_3 0 supportus 0
simqi, pr


********************Run models with q6 as dep var**********************************
****************************************************************************************
use temp5, clear

estsimp ologit q6a foreign q3a_1 q3a_2 q3a_7 _Iinstitut_2 _Iinstitut_3 supportus forgovpos

**change foreign, supportus==1 **

setx foreign min forgovpos median q3a_1 median q3a_2 median q3a_7 median  _Iinstitut_2 0  _Iinstitut_3 0 supportus 1
simqi, pr

setx foreign mean forgovpos median q3a_1 median q3a_2 median q3a_7 median  _Iinstitut_2 0  _Iinstitut_3 0 supportus 1
simqi, pr

setx foreign max forgovpos median q3a_1 median q3a_2 median q3a_7 median  _Iinstitut_2 0  _Iinstitut_3 0 supportus 1
simqi, pr

**change foreign, supportus==0**

setx foreign min forgovpos median q3a_1 median q3a_2 median q3a_7 median  _Iinstitut_2 0  _Iinstitut_3 0 supportus 0
simqi, pr

setx foreign mean forgovpos median q3a_1 median q3a_2 median q3a_7 median  _Iinstitut_2 0  _Iinstitut_3 0 supportus 0
simqi, pr

setx foreign max forgovpos median q3a_1 median q3a_2 median q3a_7 median  _Iinstitut_2 0  _Iinstitut_3 0 supportus 0
simqi, pr

**change q3a_7, supportus==0, foreign==mean**

setx foreign mean forgovpos median  q3a_1 median q3a_2 median q3a_7 -1  _Iinstitut_2 0  _Iinstitut_3 0 supportus 0
simqi, pr

setx foreign mean forgovpos median q3a_1 median q3a_2 median q3a_7 1  _Iinstitut_2 0  _Iinstitut_3 0 supportus 0
simqi, pr


*****************************************************************************
*****************************************************************************

* use temp3 data again to create summary stats
use temp3, clear

gen q6sq=1 if q6a==0
gen q6some=1 if q6a==.3
gen q6all=1 if q6a==.6

* Count the total number of delegates from each country who prefer each outcome

bys country: egen totq6sq=sum(q6sq)
bys country: egen totq6some=sum(q6some)
bys country: egen totq6all=sum(q6all)


collapse totq6sq totq6some totq6all, by(country)

gen totcountry=totq6sq+totq6some+totq6all

* Here I list the totals for all countries for which there is more than one delegate

list country totq6sq totq6some totq6all if totcountry>1 

* Calculate Chi2 test. The idea is that if member state does not affect delegate's choice over q6a
* there should be an equal number of delegates from each country that prefer each outcome.
* In other words, delegates from the same country should have different preferences if member state
* delegations do not matter.

gen exp=totcountry/3
gen chi1=((totq6sq-exp)^2)/exp
gen chi2=((totq6some-exp)^2)/exp
gen chi3=((totq6all-exp)^2)/exp

gen chi=chi1+chi2+chi3
egen sumchi=sum(chi) if exp>=1


* Here I do the same thing again, but I check whether party matters, rather than 
* member state delegation

use temp3, clear
gen q6sq=1 if q6a==0
gen q6some=1 if q6a==.3
gen q6all=1 if q6a==.6

* count the number of members on each outcome for q6 by party id

bys part_id: egen totq6sq=sum(q6sq)
bys part_id: egen totq6some=sum(q6some)
bys part_id: egen totq6all=sum(q6all)

gen totalpartymem=totq6sq+totq6some+totq6all

collapse totq6sq totq6some totq6all country totalpartymem, by(part_id)

* list parties for which there are more than one delegate

list part_id country totq6sq totq6some totq6all if totalpartymem>1

************************************************************************
************************************************************************
*List delegates by type (using cfspindex)
**********************************************************
sort country_name name

*Ambivalent nationalists
list country_name name party if cfspindex<0 & q6a>.5

*Nationalists
list country_name name party if cfspindex<0 & q6a<.5

*Internationalists
list country_name name party if cfspindex>0 & q6a>.5

*Ambivalent internationalists
list country_name name party if cfspindex>0 & q6a<.5

list country_name name party if cfspindex==0

*********************************************************
* List delegates by type (using Chapel Hill foreign variable)
*********************************************************

*Ambivalent nationalists
list country_name name party if foreign<5.1 & q6a>.5

*Nationalists
list country_name name party if foreign<5.1 & q6a<.5

*Internationalists
list country_name name party if foreign>=5.1 & q6a>.5

*Ambivalent internationalists
list country_name name party if foreign>=5.1 & q6a<.5

list country_name name party if foreign==5.1
